package com.njzxw.fs.commonutil.util;

import org.springframework.stereotype.Service;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class StringHelper {

	public static String getRandomStr() {
		Random random = new Random();
		// 取随机产生的认证码(6位数字)
		String sRand = "";
		for (int i = 0; i < 6; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
		}
		return sRand;
	}
	
	public static String get (Map map, String keyName){
		if(map == null){
			return "";
		}
		return notEmpty(map.get(keyName));
	}
	
	public static String getTrim (Map map, String keyName){
		return notEmpty(map.get(keyName)).trim();
	}
	
	public static String notEmpty (Object value){
		if (value == null){
			value = "";
		}
		return String.valueOf(value);
	}
	
	/**
	 * 转换执行的sql 为直接文本显示
	 * @param sql
	 * @param params
	 * @return
	 */
	public static String getSql(String sql,Map<String,?> params){
		Pattern p=Pattern.compile(":[0-9a-zA-Z_-]*");  
		Matcher m=p.matcher(sql); 
		while(m.find()){
			String key = m.group();
			String valu = get(params, key.replace(":",""));
			if(params.containsKey(key.replace(":",""))){
				sql = sql.replace(key, "'"+valu+"'");
			}
		}
		return sql;
	}
	
	public static List distinctList (List list, final String key, final String orderKey)
	{
		List<Map> lst = (List) byteClone(list);
		if (orderKey != null) Collections.sort(lst, new Comparator<Map>()
		{
			public int compare (Map m1, Map m2)
			{
				if (m1.get(orderKey) instanceof Number)
				{
					BigDecimal n1 = (BigDecimal) m1.get(orderKey);
					BigDecimal n2 = (BigDecimal) m1.get(orderKey);
					return n1.compareTo(n2);
				}
				else
				{
					String s1 = get(m1, orderKey);
					String s2 = get(m2, orderKey);
					return s1.compareTo(s2);
				}
			}
		});
		String temp = null;
		for (Iterator<Map> iter = lst.iterator(); iter.hasNext();)
		{
			Map mapCur = iter.next();
			String curValue = get(mapCur, key);
			if (curValue.equals(temp)) iter.remove();
			else
				temp = curValue;
		}
		return lst;
	}
	
	public static final Object byteClone(Object src) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(baos);
			out.writeObject(src);
			out.close();
			ByteArrayInputStream bin = new ByteArrayInputStream(baos
					.toByteArray());
			ObjectInputStream in = new ObjectInputStream(bin);
			Object clone = in.readObject();
			in.close();
			return (clone);
		} catch (ClassNotFoundException e) {
			throw new InternalError(e.toString());
		} catch (StreamCorruptedException e) {
			throw new InternalError(e.toString());
		} catch (IOException e) {
			throw new InternalError(e.toString());
		}
	}

	public static final char UNDERLINE='_';

	/**
	 * 驼峰转下划线
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (Character.isUpperCase(c)){
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线转驼峰
	 *
	 * @param param
	 * @return
	 */
	public static String underlineToCamel(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (c==UNDERLINE){
				if (++i<len){
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static String underlineToCamel2(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		StringBuilder sb=new StringBuilder(param);
		Matcher mc= Pattern.compile("_").matcher(param);
		int i=0;
		while (mc.find()){
			int position=mc.end()-(i++);
			//String.valueOf(Character.toUpperCase(sb.charAt(position)));
			sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String sql = "fdsafdsafdsa, fdsa,:gfdd, gf :121ds afds ";
//		Pattern p=Pattern.compile(":[0-9a-zA-Z]*");  
//		Matcher m=p.matcher(sql);  
//		while(m.find()){
//			sql = sql.replace(m.group(), "-");
//			System.out.println(sql);  
//		} 
//		System.out.println(sql);
		Map<String, String> params = new HashMap<String, String>();
		params.put("gfdd", "12");
		params.put("121ds", "12");
		
		System.out.println(StringHelper.getSql(sql, params));
		System.out.println(sql);
	}
	
}
